import csv
import re
import os

import requests
from bs4 import BeautifulSoup


def get_content_write_to_local():
    url = 'http://www.cntour.cn/'
    strhtml = requests.get(url)
    soup = BeautifulSoup(strhtml.text, 'lxml')

    data = soup.select('#main>div>div.mtop.firstMod.clearfix>div.centerBox>ul.newsList>li>a')

    result = []
    for item in data:
        result.append({
            'title': item.get_text(),
            'link': item.get('href'),
            'ID': re.findall('\d+', item.get('href'))[0]
        })
    print(result)

    print("准备保存到本地...")
    folder_path = '../../resources/csv'
    filename = '../../resources/csv/cntour.csv'
    if not os.path.exists(folder_path):os.makedirs(folder_path)

    f = open(filename, 'w', encoding='utf-8')
    csv_writer = csv.writer(f)
    csv_writer.writerow(["title", "link", "ID"])
    for o in result:
        csv_writer.writerow([o.get('title'), o.get('link'), o.get('ID')])
    f.close()
    print("保存到本地成功!")


if __name__ == '__main__':
    get_content_write_to_local()
